package com.demo.controller;

import com.demo.entity.Products;
import com.demo.service.ProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;


@Controller					//加入IOC容器
@RequestMapping("controller")
public class ProductsController {
	//控制器依赖于Service
	@Autowired
	@Qualifier("productsService")
	private ProductsService productsService;
	public void setProductsService(ProductsService productsService) {
		this.productsService = productsService;
	}

	//查询所有商品信息列表
	@RequestMapping({"product_list_action","productsInfo_action","adminOrder"})
	public String queryAllProdectsInfo(HttpServletRequest request){
		String uri = request.getRequestURI();
		List<Products> productsInfo = productsService.queryProdectsInfo();
		request.getSession().setAttribute("productsInfoList", productsInfo);
		System.out.println("查询所有商品信息列表======");
		if(uri.endsWith("/product_list_action")){
			return "product_list";
		}else if(uri.endsWith("productsInfo_action")){
			return "store_product_list";
		}else if(uri.endsWith("adminOrder")){
			return "orderListByAdmin";
		}
		return null;
	}
	//分页查询商品信息列表
	@RequestMapping({"product_list_page_action","product_list_before_action","product_list_after_action"})
	public String queryPageProdectsInfo(HttpServletRequest request){
		int pnum = 0;
		String uri = request.getRequestURI();
		Integer snum = (Integer) request.getSession().getAttribute("PNUM");
		if(snum == null){
			pnum = 0;
		}

		if(uri.endsWith("product_list_before_action")){
			if(snum==0){
				pnum = 0;
			}else if(snum-5 >= 0){
				pnum = snum-5;
			}
		}else if(uri.endsWith("product_list_after_action")){
			if(snum==0){
				pnum = 5;
			}else{
				pnum = snum+5;
			}
		}
		request.getSession().setAttribute("PNUM", pnum);
		List<Products> productsInfo = productsService.queryPageProdectsInfo(pnum);

		request.getSession().setAttribute("productsInfoList", productsInfo);

		System.out.println("分页查询商品信息列表======");

		return "product_list";
	}

	//删除商品
	@RequestMapping("deleteProductInfo/{pid}")
	public String deleteProductInfo(@PathVariable("pid")String pid){
		productsService.deleteProductInfo(pid);
		System.out.println("删除商品cg...................");
		return "redirect:/controller/productsInfo_action";
	}

	//增加商品
	@RequestMapping("addProductAction")
	public String addProductInfo(HttpServletRequest req) throws UnsupportedEncodingException{
		String pname = new String(req.getParameter("pname").getBytes("iso-8859-1"),"UTF-8");
		String pprice = req.getParameter("pprice");
		int pnum = Integer.parseInt(req.getParameter("pnum"));
		String pcategory = new String(req.getParameter("pcategory").getBytes("iso-8859-1"),"UTF-8");
		String pdescription = new String(req.getParameter("pdescription").getBytes("iso-8859-1"),"UTF-8");
		System.out.println(pname+pprice+pnum+pcategory+pdescription);
		productsService.insertProductInfo(pname, pprice, pnum, pcategory, pdescription);
		System.out.println("增加商品cg...................");
		req.setAttribute("addMessage", "商品增加成功 ! ");
		return "addProduct";
	}

	//获取单一商品信息
	@RequestMapping("updateOneProduct/{pid}")
	public String updateOneProduct(@PathVariable("pid")String pid,HttpServletRequest req) throws UnsupportedEncodingException{
		List<Products> productsList =  (List<Products>) req.getSession().getAttribute("productsInfoList");
		Products pds = new Products();
		for (Products productsInfoList : productsList) {
			if(Integer.parseInt(pid) == productsInfoList.getProduct_id()){
				pds.setProduct_id(Integer.parseInt(pid));
				pds.setName(productsInfoList.getName());
				pds.setPnum(productsInfoList.getPnum());
				pds.setPrice(productsInfoList.getPrice());
				pds.setCategory(productsInfoList.getCategory());
				pds.setDescription(productsInfoList.getDescription());
			}
		}
		req.getSession().setAttribute("pds", pds);
		return "updateProduct";
	}

	//修改商品
	@RequestMapping("updateProductAction/{pid}")
	public String updateProductAction(@PathVariable("pid")String pid,HttpServletRequest req) throws UnsupportedEncodingException{
		String pname = new String(req.getParameter("pname").getBytes("iso-8859-1"),"UTF-8");
		String pprice = req.getParameter("pprice");
		String pnum = req.getParameter("pnum");
		String pcategory = new String(req.getParameter("pcategory").getBytes("iso-8859-1"),"UTF-8");
//		String pdescription = req.getParameter("pdescription");
		String pdescription = new String(req.getParameter("pdescription").getBytes("iso-8859-1"),"UTF-8");
		System.out.println("pname："+pname+"\tpprice："+pprice+"\tpnum："+pnum+"\tpcategory："+pcategory+"\tpdescription："+pdescription);

		int pn = 0;

		Products pdsList =  (Products) req.getSession().getAttribute("pds");
		int id = pdsList.getProduct_id();
		String name = pdsList.getName();
		String price = Double.toString(pdsList.getPrice());
		int num = pdsList.getPnum();
		String category = pdsList.getCategory();
		String description = pdsList.getDescription();
		System.out.println("id："+id+"\tname："+name+"\tprice："+price+"\tnum："+num+"\tcategory："+category+"\tdescription："+description);

		if(pname.isEmpty()){
			pname = name;
		}
		if(pprice == ""){
			pprice = price;
		}
		if(pcategory.isEmpty()){
			pcategory = category;
		}
		if(pdescription.isEmpty()){
			pdescription = description;
			System.out.println(pdescription+"---------------"+description);
		}

		if(pnum == ""){
			pn = num;
		}else{
			pn = Integer.parseInt(pnum);
		}

		System.out.println("pname："+pname+"\tpprice："+pprice+"\tpn："+pn+"\tpcategory："+pcategory+"\tpdescription："+pdescription);

		productsService.updateProductInfo(pname, pprice, pn, pcategory, pdescription, pid);
		System.out.println("修改商品cg...................");
		req.setAttribute("addMessage", "商品信息修改成功 ! ");
		return "updateProduct";
	}

}